#-*- coding:utf-8 -*-

def insert(sql):
    """
    function:该函数的功能是进行文本插入操作
    :param sql: 用户传进来的sql语句
    :return: 无
    """
    sql_list = sql.split()
    #print (sql)
    db_name,table_name = sql_list[2].split(".")
    #获取最后一行代码的ID
    last_id = ""
    with open("%s.%s"%(db_name,table_name),"r") as fr:
        content_list = fr.readlines()
        last_record = content_list[-1]
        last_id = last_record.split(",")[0]
    #拼出最后一行记录,在操作的过程中注意insert的返回值是None
    record_list = sql_list[-1].split(",")
    record_list.insert(0,str(int(last_id)+1))
    record_line = ",".join(record_list)

    #向列表中写入记录
    with open("%s.%s"%(db_name, table_name),"a") as fw:
        fw.write("\n"+record_line)
        #记住要刷新纪录
        fw.flush()

if __name__ == '__main__':
    while True:
        sql = raw_input("sql> ").strip()
        #print sql
        insert(sql)